package com.wzc.hc.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wzc.hc.entity.po.Community;
import com.wzc.hc.entity.resp.Result;
import com.wzc.hc.entity.tool.BeanConverter;
import com.wzc.hc.entity.vo.CommunityVo;
import com.wzc.hc.service.CommunityService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/*
    @author: m1825
    @date: 2025/3/2 15:37
*/
@Tag(name = "小区")
@RestController
@RequestMapping("/community")
public class CommunityController {
    @Resource
    private CommunityService communityService;

    @Operation(summary = "新增")
    @PostMapping("/add")
    public Result<Object> add(@RequestBody CommunityVo communityVo) {
        if (communityService.save(BeanConverter.convertVoToPo(communityVo, Community.class))) return Result.ok();
        else return Result.fail();
    }

    @Operation(summary = "分页查看")
    @GetMapping("/page")
    public Result<Page<CommunityVo>> page(@RequestParam(value = "pageNum", defaultValue = "1") int pageNum,
                                          @RequestParam(value = "pageSize", defaultValue = "5") int pageSize) {
        try {
            Page<Community> page = new Page<>(pageNum, pageSize);
            Page<Community> communityPage = communityService.page(page);
            List<CommunityVo> communityVos = new ArrayList<>();
            Page<CommunityVo> communityVoPage = new Page<>(pageNum, pageSize, communityPage.getTotal());
            communityPage.getRecords().forEach(community -> {
                communityVos.add(BeanConverter.convertPoToVo(community, CommunityVo.class));
            });
            communityVoPage.setRecords(communityVos);
            return Result.ok(communityVoPage);
        } catch (Exception e) {
            return Result.fail();
        }
    }

    @Operation(summary = "删除")
    @DeleteMapping("/delete")
    public Result<Boolean> delete(@RequestParam(value = "CommunityId") String id) {
        try {
            return Result.ok(communityService.removeById(id));
        } catch (Exception e) {
            return Result.fail();
        }
    }

    @Operation(summary = "更新")
    @PutMapping("/update")
    public Result<Boolean> update(@RequestBody CommunityVo communityVo) {
        try {
            return Result.ok(communityService.updateById(BeanConverter.convertVoToPo(communityVo, Community.class)));
        } catch (Exception e) {
            return Result.fail();
        }
    }

    @Operation(summary = "查询")
    @PostMapping("/select")
    public Result<Page<CommunityVo>> select(@RequestBody CommunityVo communityVo,
                                            @RequestParam(value = "pageNum", defaultValue = "1") int pageNum,
                                            @RequestParam(value = "pageSize", defaultValue = "5") int pageSize) {
        try {
            Community community = BeanConverter.convertVoToPo(communityVo, Community.class);
            Page<Community> page = new Page<>(pageNum, pageSize);
            QueryWrapper<Community> communityQueryWrapper = new QueryWrapper<>();
            if (community.getId() != null)
                communityQueryWrapper.eq("id", community.getId());
            if (community.getCommunityName() != null)
                communityQueryWrapper.like("community_name", community.getCommunityName());
            if (community.getCommunityRegion() != null)
                communityQueryWrapper.like("community_region", community.getCommunityRegion());
            Page<Community> communityPage = communityService.page(page, communityQueryWrapper);
            List<CommunityVo> communityVos = communityPage.getRecords().stream()
                    .map(community1 -> BeanConverter.convertPoToVo(community1, CommunityVo.class))
                    .toList();
            Page<CommunityVo> communityVoPage = new Page<>(pageNum, pageSize, communityPage.getTotal());
            communityVoPage.setRecords(communityVos);
            return Result.ok(communityVoPage);
        } catch (Exception e) {
            return Result.fail();
        }
    }
}